package org.hyl.javabase.多线程.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import org.junit.Test;

public class DemoCallable {
	//	@Test
	public void test1() throws Exception {
		//第二种方式		 
		FutureTask<Integer> futureTask = new FutureTask<Integer>(new CallableThread());
		Thread thread = new Thread(futureTask);
		thread.start();
		System.out.println("result:" + futureTask.get());
	}

	//	@Test
	public void test2() throws Exception {
		/* // 第一种方式：通过线程池来获取线程 */
		ExecutorService executor = Executors.newCachedThreadPool();//创建线程池
		Future<Integer> future = executor.submit(new CallableThread());//ExecutorService中的submit方法接受一个Runnable或者Callable，然后返回一个Future来获得任务的执行结果或者取消任务。
		System.out.println("result:" + future.get());
		executor.shutdown();

	}

	@Test
	//对比test4
	public void test3() throws Exception {
		List<Future<String>> results = new ArrayList<Future<String>>();
		ExecutorService es = Executors.newFixedThreadPool(10);
		//ExecutorService es = Executors.newCachedThreadPool();
		for (int i = 0; i < 50; i++)
			results.add(es.submit(new Task(i))); //可以取到正确的值
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (Future<String> res : results)
			System.out.println(res.get());
		es.shutdown();
	}

	//@Test
	public void test4() throws Exception {
		List<String> results = new ArrayList<String>();
		ExecutorService es = Executors.newFixedThreadPool(5);

		//ExecutorService es = Executors.newCachedThreadPool();
		for (int i = 0; i < 50; i++) {
			Task2 t = new Task2(String.valueOf(i));
			es.execute(t);
			results.add(t.get());//无法取到正确的值
		}
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//输出结果又异常
		for (String res : results)
			System.out.println(res);

		es.shutdown();
	}

}

class Task implements Callable<String> {
	Integer _i = 0;

	Task(Integer i) {
		
		_i = i;
	}

	@Override
	public String call() throws Exception {

		String tid = String.valueOf(Thread.currentThread().getId());
		System.out.printf("Thread#%s : in call\n", tid);
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "th:" + tid + ',' + _i.toString();
	}
}

class Task2 implements Runnable {
	String _i = "";

	Task2(String i) {
		
		_i = i;
	}

	public String get() {
		return _i;
	}

	@Override
	public void run() {
	
		String tid = String.valueOf(Thread.currentThread().getId());
	
		System.out.printf("Thread#%s : in call\n", tid);
		try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		_i=tid+':'+_i;
	}
}
